<template>
  <div class="bg-gray">
    <b-container>
      <b-row>
        <b-col>
          <b-breadcrumb class="bg-gray bread-border">
            <b-breadcrumb-item to="/space">
              <b-icon icon="house-fill" scale="1.25" shift-v="1.25"/>
              个人空间
            </b-breadcrumb-item>
            <b-breadcrumb-item to="/publish" active>
              <b-icon icon="list-ul" scale="1.25" shift-v="-1.25"/>
              发布列表
            </b-breadcrumb-item>
          </b-breadcrumb>
        </b-col>
      </b-row>
      <b-row>
        <transaction-item
            v-for="(item,index) in items"
            :item="item"
            @publish="changestatus(1,index)"
            @dispublish="changestatus(0,index)"
            @delete="deleteitem(index)"
            @update="update(index)"
            @click="gotodetail(index)"
        ></transaction-item>
        <b-col class="mb-2" cols="12" md="6">
          <b-button block variant="primary" to="/publish/newpub">新建发布</b-button>
        </b-col>
      </b-row>

    </b-container>
  </div>
</template>

<script>
  import TransactionItem from "../components/TransactionItem";
  import urlconfig from "../urlconfig";

  export default {
    name: "NewPublish",
    components:{TransactionItem},
    data(){
      return {
        // imgsrc:require("../img/1.jpg"),
        // item:{
        //   sellervariant:'primary',
        //   selleravatarsrc:require('../img/avatar.jpg'),
        //   sellernick:'zzn',
        //   imgsrc:require("../img/1.jpg"),
        //   title:'小米二级耳机',
        //   desc:'asdfasdf按隧洞官方色萨热 但是如个人r',
        //   price:'2345',
        //   status:1
        //
        // },
        items:[]
      }
    },
    methods:{
      init(){
        let data ={
          uuid:this.$store.state.uuid,
          level:4
        }
        this.$ajax.post("/item/listitems",data).then(resp=>{
          let data = resp.data
          if (data.code === 200){
            // console.log(data.data)
            let ites = data.data.items
            let userinfo = data.data.userinfo
            for (let i in data.data.items) {
              let item = {
                sellervariant: userinfo.avatarcolor,
                selleravatarsrc:urlconfig.baseUrl + urlconfig.avatarResource +
                userinfo.avatarpath,
                sellernick:userinfo.nickname,
                selleruuid:userinfo.uuid,
                imgsrc: urlconfig.baseUrl + urlconfig.fileResources +
                  ites[i].files[0].filename,
                title: ites[i].title,
                desc: ites[i].description,
                price: ites[i].price,
                status: ites[i].status,
                itemid:ites[i].itemid

              }
              this.items.push(item)
            }

          }
        }).catch(err=>{
          this.makeToast('请求失败',false)
        })
      },

      changestatus(s,index){

        let data={
          itemid:this.items[index].itemid,
          status:s
        }
        this.$ajax.post('/item/updateStatus',data).then(resp=>{
          let data = resp.data
          if (data.code === 200){
            this.makeToast(data.msg)
            this.items[index].status=data.data
          }else{
            this.makeToast(data.msg,false)
          }
        }).catch(err=>{
          this.makeToast('请求失败',false)
        })
      },
      deleteitem(index){

        let data={
          itemid:this.items[index].itemid
        }
        this.$ajax.post('/item/deleteItem',data).then(resp=>{
          let data = resp.data
          if (data.code === 200){
            this.makeToast(data.msg)
            this.items.splice(index,1)
          }else{
            this.makeToast(data.msg,false)
          }
        }).catch(err=>{
          this.makeToast('请求失败',false)
        })
      },
      gotodetail(index){
        this.$router.push('/publish/'+this.items[index].itemid)
      },
      update(index){
        this.$router.push('/publish/modify/'+this.items[index].itemid)

      },
      makeToast(msg, res = true) {
        this.$bvToast.toast(msg, {
          title: res ? '操作成功!' : '操作失败！',
          variant: res ? 'primary' : 'danger',
          solid: true,
          autoHideDelay: 5000,
        })
      }
    },
    mounted() {
      this.init()
    }
  }
</script>

<style scoped>

</style>